/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.managed;

import beans.custom.GameController;
import beans.ejb.FantasyleagueFacade;
import beans.ejb.FantasyteamFacade;
import beans.ejb.FantasyuserFacade;
import beans.ejb.WeekFacade;
import com.sun.faces.context.ApplicationMap;
import entities.Fantasyleague;
import entities.Fantasyteam;
import entities.Fantasyuser;
import entities.Week;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author The Lunatics
 */
@ManagedBean
@SessionScoped
public class AccountBean implements Serializable {

    private Fantasyuser account;
    private List<Fantasyteam> myTeams;
    private List<Fantasyleague> myLeagues;
    private HtmlDataTable leagueDataTable;
    private Fantasyleague currentLeague;
    private Fantasyuser currentLeagueOwner;
    private Fantasyteam team1;
    private Fantasyteam team2;
    private Fantasyteam team3;
    private Fantasyteam team4;
    private Fantasyteam team5;
    private Fantasyteam team6;
    private Fantasyteam team7;
    private Fantasyteam team8;
    private Fantasyteam currentTeam;
    private Integer id;
    private String loginName;
    private String email;
    private String password;
    private boolean isLogin = false;
    private String newLoginName;
    private String newEmail;
    private String newPassword;
    private String rePassword;
    private String checkLoginNameString;
    private String checkEmailString;
    private String checkPasswordString;
    private String loginNameColor;
    private String emailColor;
    private String passwordColor;
    private boolean checkLoginName = true;
    private boolean checkEmail = true;
    private boolean checkPassword = true;
    private Week currentWeek;
    @EJB
    private FantasyuserFacade userFacade;
    @EJB
    private FantasyteamFacade teamFacade;
    @EJB
    private FantasyleagueFacade leagueFacade;
    @EJB
    private WeekFacade weekFacade;

    public String getPasswordColor() {
        return passwordColor;
    }

    public void setPasswordColor(String passwordColor) {
        this.passwordColor = passwordColor;
    }

    public String getCheckPasswordString() {
        return checkPasswordString;
    }

    public void setCheckPasswordString(String checkPasswordString) {
        this.checkPasswordString = checkPasswordString;
    }

    public String getRePassword() {
        return rePassword;
    }

    public void setRePassword(String rePassword) {
        this.rePassword = rePassword;
    }

    public HtmlDataTable getLeagueDataTable() {
        return leagueDataTable;
    }

    public void setLeagueDataTable(HtmlDataTable dataTable) {
        this.leagueDataTable = dataTable;

    }

    public void logout() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(true);

        session.invalidate();
    }

    public String selectCurrentLeague() {
        currentLeague = (Fantasyleague) leagueDataTable.getRowData();
        System.err.println("current league changed to: " + currentLeague);
        return "league";
    }

    public void login() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();


        this.account = userFacade.getUserByLoginName(loginName, password);

        if (account.getPassword().equals(password)) {
            isLogin = true;
        } else {
            isLogin = false;
        }

        this.myTeams = teamFacade.getUsersTeams(account);
        this.myLeagues = leagueFacade.getUsersLegues(account);

        setCurrentLeagueMember();
        currentWeek = weekFacade.getWeek("2011", "01");
        this.id = account.getId();
    }

    public void setCurrentLeagueMember() {
        if (myLeagues.size() > 0) {
            currentLeague = myLeagues.get(0);
            currentLeagueOwner = currentLeague.getOwnerUserId();
            team1 = teamFacade.getTeamX(currentLeague, 1, currentLeague.getMaximumNumberOfUsers());
            team2 = teamFacade.getTeamX(currentLeague, 2, currentLeague.getMaximumNumberOfUsers());
            team3 = teamFacade.getTeamX(currentLeague, 3, currentLeague.getMaximumNumberOfUsers());
            team4 = teamFacade.getTeamX(currentLeague, 4, currentLeague.getMaximumNumberOfUsers());
            team5 = teamFacade.getTeamX(currentLeague, 5, currentLeague.getMaximumNumberOfUsers());
            team6 = teamFacade.getTeamX(currentLeague, 6, currentLeague.getMaximumNumberOfUsers());
            team7 = teamFacade.getTeamX(currentLeague, 7, currentLeague.getMaximumNumberOfUsers());
            team8 = teamFacade.getTeamX(currentLeague, 8, currentLeague.getMaximumNumberOfUsers());
        }
    }

    public String createAccount() throws IOException {
        if (!checkLoginName && !checkEmail) {
            account = new Fantasyuser();
            account.setLoginName(newLoginName);
            account.setPassword(newPassword);
            account.setEmail(newEmail);
            userFacade.createNewAccount(account);
            return "index";
        } else {
            return "createAccount";
        }
    }

    public Fantasyuser getUser() {
        return account;
    }

    public void setUser(Fantasyuser account) {
        this.account = account;
    }

    /**
     * Creates a new instance of AccountBean
     */
    public AccountBean() {
    }

    public Fantasyuser getAccount() {
        return account;
    }

    public void setAccount(Fantasyuser account) {
        this.account = account;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isIsLogin() {
        return isLogin;
    }

    public void setIsLogin(boolean isLogin) {
        this.isLogin = isLogin;
    }

    public FantasyuserFacade getUserFacade() {
        return userFacade;
    }

    public void setUserFacade(FantasyuserFacade userFacade) {
        this.userFacade = userFacade;
    }

    public String getLoginNameColor() {
        return loginNameColor;
    }

    public void setLoginNameColor(String loginNameColor) {
        this.loginNameColor = loginNameColor;
    }

    public String getEmailColor() {
        return emailColor;
    }

    public void setEmailColor(String emailColor) {
        this.emailColor = emailColor;
    }

    public String getCheckEmailString() {
        return checkEmailString;
    }

    public void setCheckEmailString(String checkEmailString) {
        this.checkEmailString = checkEmailString;
    }

    public String getCheckLoginNameString() {
        return checkLoginNameString;
    }

    public void setCheckLoginNameString(String checkLoginNameString) {
        this.checkLoginNameString = checkLoginNameString;
    }

    public String getNewLoginName() {
        return newLoginName;
    }

    public void setNewLoginName(String newLoginName) {
        this.newLoginName = newLoginName;
    }

    public String getNewEmail() {
        return newEmail;
    }

    public void setNewEmail(String newEmail) {
        this.newEmail = newEmail;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public String createAccountPage() {
        return "createAccount";
    }

    public void verifyLoginName() {
        if (userFacade.isLoginNameOccupied(newLoginName) || newLoginName.equals("")) {
            checkLoginNameString = "The loginname is occupied.";
            checkLoginName = true;
            loginNameColor = "red";
        } else {
            checkLoginNameString = "The loginname is available.";
            checkLoginName = false;
            loginNameColor = "blue";
        }
    }

    public void verifyEmail() {
        if (userFacade.isEmailOccupied(newEmail) || newEmail.equals("")) {
            checkEmailString = "The email is occupied.";
            checkEmail = true;
            emailColor = "red";
        } else {
            checkEmailString = "The email is available.";
            checkEmail = false;
            emailColor = "blue";
        }
    }

    public void verifyPassword() {
        if (newPassword == null || rePassword == null || newPassword.equals("") || rePassword.equals("")) {
            checkPassword = true;
            checkPasswordString = "Wrong";
            passwordColor = "red";
        } else if (newPassword.equals(rePassword)) {
            checkPassword = false;
            checkPasswordString = "Right";
            passwordColor = "blue";
        } else {
            checkPassword = true;
            checkPasswordString = "Wrong";
            passwordColor = "red";
        }
    }

    public List<Fantasyteam> getMyTeams() {
        return myTeams;
    }

    public void setMyTeams(List<Fantasyteam> myTeams) {
        this.myTeams = myTeams;
    }

    public List<Fantasyleague> getMyLeagues() {
        if (myLeagues == null) {
            if (account == null) {
                System.err.println("USER IS NULL!");
            }
            myLeagues = leagueFacade.getUsersLegues(account);
        }
        return myLeagues;
    }

    public void setMyLeagues(List<Fantasyleague> myLeagues) {
        this.myLeagues = myLeagues;
    }

    public Fantasyleague getCurrentLeague() {
        return currentLeague;
    }

    public void setCurrentLeague(Fantasyleague currentLeague) {
        this.currentLeague = currentLeague;
    }

    public Fantasyteam getTeam1() {
        return team1;
    }

    public void setTeam1(Fantasyteam team1) {
        this.team1 = team1;
    }

    public Fantasyteam getTeam2() {
        return team2;
    }

    public void setTeam2(Fantasyteam team2) {
        this.team2 = team2;
    }

    public Fantasyteam getTeam3() {
        return team3;
    }

    public void setTeam3(Fantasyteam team3) {
        this.team3 = team3;
    }

    public Fantasyteam getTeam4() {
        return team4;
    }

    public void setTeam4(Fantasyteam team4) {
        this.team4 = team4;
    }

    public Fantasyteam getTeam5() {
        return team5;
    }

    public void setTeam5(Fantasyteam team5) {
        this.team5 = team5;
    }

    public Fantasyteam getTeam6() {
        return team6;
    }

    public void setTeam6(Fantasyteam team6) {
        this.team6 = team6;
    }

    public Fantasyteam getTeam7() {
        return team7;
    }

    public void setTeam7(Fantasyteam team7) {
        this.team7 = team7;
    }

    public Fantasyteam getTeam8() {
        return team8;
    }

    public void setTeam8(Fantasyteam team8) {
        this.team8 = team8;
    }

    public boolean isCheckLoginName() {
        return checkLoginName;
    }

    public void setCheckLoginName(boolean checkLoginName) {
        this.checkLoginName = checkLoginName;
    }

    public boolean isCheckEmail() {
        return checkEmail;
    }

    public void setCheckEmail(boolean checkEmail) {
        this.checkEmail = checkEmail;
    }

    public FantasyteamFacade getTeamFacade() {
        return teamFacade;
    }

    public void setTeamFacade(FantasyteamFacade teamFacade) {
        this.teamFacade = teamFacade;
    }

    public FantasyleagueFacade getLeagueFacade() {
        return leagueFacade;
    }

    public void setLeagueFacade(FantasyleagueFacade leagueFacade) {
        this.leagueFacade = leagueFacade;
    }

    public Fantasyuser getCurrentLeagueOwner() {
        return currentLeagueOwner;
    }

    public void setCurrentLeagueOwner(Fantasyuser currentLeagueOwner) {
        this.currentLeagueOwner = currentLeagueOwner;
    }

    public boolean isCheckPassword() {
        return checkPassword;
    }

    public void setCheckPassword(boolean checkPassword) {
        this.checkPassword = checkPassword;
    }

    public Week getCurrentWeek() {
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext extContext = context.getExternalContext();
        GameController gameController = (GameController) extContext.getApplicationMap().get("gameController");
        gameController.getCurrentWeek();
        return currentWeek;
    }

    public void setCurrentWeek(Week currentWeek) {
        this.currentWeek = currentWeek;
    }
}
